//---------------------------------------------------------------
//
//				Supper AB530x
//huajun @ 2019-05-30
/* timer 使用情况
timer0 : 系统时钟使用
timer1:  (可选)  ledseg_ajust.  7脚屏的时候使用(亮度均匀调节).  timer1_irq_init 
timer2:  (可选)    注意timer2和timer4/5用同一个中断向量入口, 需要timer2及tws时的话, 请查看tws_timer2_isr_callback.  不要重新注册timer2, 会覆盖掉以前的timer4/5中断向量. 
timer3:  (可选)    软件红外解码使用...
timer4/timer5 : tws使用用了.  

timer3/timer4/timer5 只有可以产生PWM
*/
#include "include.h"
#include "port_timer_ex.h"

#if 0  //TIMER_PWM_EXAMPLE

/*******************************************************************************
timer isr
*******************************************************************************/
AT(.com_text.timer.isr)
static isr_t timerx_isr_user_function[6];
AT(.com_text.timer)
void timer1_ex_isr(void)
{
	if (TMR1CON & BIT(9)){
		TMR1CPND = BIT(9);				//Clear Pending
		(*timerx_isr_user_function[1])();
	}
}

#if (!IRRX_SW_EN)
AT(.com_text.timer)
static void timer3_ex_isr(void)
{
	if (TMR3CON & BIT(16)){
		TMR3CPND = BIT(16);				//Clear Pending
		(*timerx_isr_user_function[3])();
	}
}
#endif

#if (!BT_TWS_EN)
AT(.com_text.timer)
static void timer245_ex_isr(void)
{
	if (TMR2CON & BIT(9)){
		TMR2CPND = BIT(9);				//Clear Pending
		(*timerx_isr_user_function[2])();
	}

	if (TMR4CON & BIT(16)){
		TMR4CPND = BIT(16);				//Clear Pending
		(*timerx_isr_user_function[4])();
	}

	if (TMR5CON & BIT(16)){
		TMR5CPND = BIT(16);				//Clear Pending
		(*timerx_isr_user_function[5])();
	}
}
#endif


static const timer_config_strc timer_config[6] = {
	{
		.TMRxCON	= &TMR0CON,
		.TMRxCNT	= &TMR0CNT,
		.TMRxPR 	= &TMR0PR,
	},

	{
		.TMRxCON	= &TMR1CON,
		.TMRxCNT	= &TMR1CNT,
		.TMRxPR 	= &TMR1PR,
	},

	{
		.TMRxCON	= &TMR2CON,
		.TMRxCNT	= &TMR2CNT,
		.TMRxPR 	= &TMR2PR,
	},

	{
		.TMRxCON 	= &TMR3CON,
		.TMRxCNT 	= &TMR3CNT,
		.TMRxPR		= &TMR3PR,
	},

	{
		.TMRxCON	= &TMR4CON,
		.TMRxCNT	= &TMR4CNT,
		.TMRxPR 	= &TMR4PR,

	},

	{
		.TMRxCON 	= &TMR5CON,
		.TMRxCNT 	= &TMR5CNT,
		.TMRxPR		= &TMR5PR,
	},
};

static const u8 timer_isr_vector[6] = {
	IRQ_TMR1_VECTOR,
	IRQ_TMR1_VECTOR,
	IRQ_TMR245_VECTOR,
	IRQ_IRRX_VECTOR,
	IRQ_TMR245_VECTOR,
	IRQ_TMR245_VECTOR,
};


static const isr_t isr_func[6] = {
	NULL,

#if (GUI_SELECT != GUI_LEDSEG_7P7S)
	timer1_ex_isr,
#else
	NULL,
#endif

#if (!BT_TWS_EN)
	timer245_ex_isr,
#else
	NULL,
#endif

#if (!IRRX_SW_EN)
	timer3_ex_isr,
#else
	NULL,
#endif

#if (!BT_TWS_EN)
	timer245_ex_isr,
#else
	NULL,
#endif

#if (!BT_TWS_EN)
	timer245_ex_isr,
#else
	NULL,
#endif
};


/*******************************************************************************
* hw_timer_isr_setup
* Author/Date: Huajun, 30/05/2019
* DESCRIPTION: Configure timer interrupt
* INPUT: timer_id : timer moudle
		 period_us: timer isr time
		 timer_isr_function: callback
* OUTPUT: NULL
* RETURN: Success or fail.
*******************************************************************************/
s8 hw_timer_isr_setup(timer_module timer_id, u32 period_us, void(* timer_isr_function)(void))
{

	if(timer_id < TIMER1){
		return -1;
	}

#if (GUI_SELECT == GUI_LEDSEG_7P7S)
	if(timer_id == TIMER1){
		return -2;
	}
#endif

#if IRRX_SW_EN
	if(timer_id == TIMER3){
		return -4;
	}
#endif

#if BT_TWS_EN
	if((timer_id == TIMER2) || (timer_id == TIMER4) || (timer_id == TIMER5)){
		return -3;
	}
#endif

	*(timer_config[timer_id].TMRxCNT) = 0;
	*(timer_config[timer_id].TMRxPR)  = period_us-1;
	*(timer_config[timer_id].TMRxCON) = BIT(7) | BIT(2) | BIT(0);
	timerx_isr_user_function[timer_id] = timer_isr_function;
	sys_irq_init(timer_isr_vector[timer_id], 1, isr_func[timer_id]);

	return 0;
}



/*******************************************************************************
* hw_timer_isr_stop
* Author/Date: Huajun, 30/05/2019
* DESCRIPTION: stop timer interrupt
* INPUT: timer_id : timer moudle
* OUTPUT: NULL
* RETURN: Success or fail.
*******************************************************************************/
s8 hw_timer_isr_stop(timer_module timer_id)
{
	if(timer_id < TIMER1){
		return -1;
	}

#if (GUI_SELECT == GUI_LEDSEG_7P7S)
	if(timer_id == TIMER1){
		return -2;
	}
#endif

#if IRRX_SW_EN
	if(timer_id == TIMER3){
		return -4;
	}
#endif

#if BT_TWS_EN
	if((timer_id == TIMER2) || (timer_id == TIMER4) || (timer_id == TIMER5)){
		return -3;
	}
#endif

	*(timer_config[timer_id].TMRxCON) = 0x00;

	return 0;
}

#endif  //TIMER_PWM_EXAMPLE



#if BT_TWS_EN && (!BT_HFP_EN)
//TIME2被TWS双耳通话占用。有TWS但不开通话时，可以在此使用TIMER2.
//使用方法：先调用tws_timer2_init(), timer2中断起来后会自动回调tws_timer2_isr_callback。
void tws_irq_init(void);
void tws_timer2_init(void)
{
    CLKGAT0 |= BIT(25);                 //TIME2
    TMR2PR  = 1000*50 - 1;
    TMR2CNT = 0;
    TMR2CON = BIT(7) | BIT(2) | BIT(0); //interrupt en, counter mode
    tws_irq_init();
}

AT(.com_text.cst)
const char strt2_test[]="#";

//must return true, otherwise will run Tsco code
//回调前已经清除了timer2的pending,不用再做清除pending操作
AT(.com_text.timer2)
bool tws_timer2_isr_callback(void)
{
    printf(strt2_test);
    return true;
}
#endif // BT_TWS_EN
